package com.quanquan.store.controller;

import base.BaseController;
import com.quanquan.store.dto.DailyReportDTO;
import com.quanquan.store.dto.NoSupportStoreDTO;
import com.quanquan.store.dto.ReportDTO;
import com.quanquan.store.request.DailyReportRequestBody;
import com.quanquan.store.service.DailyReportService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.DateUtils;
import web.response.ResultBody;

import java.util.*;

/**
 * 功能描述:
 *
 * @ClassName: DailyReportController
 * @Author: xiaoqianh丶
 * @Date: 2019-07-02 10:18
 * @Version: V1.0
 */
@RestController
@RequestMapping("/daily_report")
public class DailyReportController extends BaseController {

    @Autowired
    private DailyReportService dailyReportService;

    /**
     * 提交每日报表
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/add")
    public ResultBody createDailyReport(@RequestBody DailyReportRequestBody requestBody) {

        Integer tradingNum = requestBody.getTradingNum();
        if(tradingNum == null){
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());

        }

        String createTime = requestBody.getCreateTime();
        //判断提交的时间在三天以内
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date createDate = DateUtils.parseStr2Date("yyyy-MM-dd", createTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.DATE, -3);
        Date checkTime = calendar.getTime();

        if(createDate.before(checkTime)){
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "只能提交3天内的数据");

        }
        String msg = dailyReportService.createDailyReport(requestBody);
        if (msg == null) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());

        }

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), msg, msg);

    }

    /**
     * 查询每日报表
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/query")
    public ResultBody queryAllDailyReport(@RequestBody DailyReportRequestBody requestBody) {
        ReportDTO allDailyReport = dailyReportService.findAllDailyReport(requestBody);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), allDailyReport);
    }

    /**
     * 查询每日报表
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/backend_query")
    @ResponseBody
    public Map<String, Object> getAll(DailyReportRequestBody requestBody) {
        Integer page = requestBody.getPage();
        if (null != page && page > 0) {
            page = page -1;
            requestBody.setPage(page);
        }
        ReportDTO allDailyReport = dailyReportService.findAllDailyReport(requestBody);
        Map<String, Object> data = new HashMap<>();
        if (null != allDailyReport) {
            data.put("size",allDailyReport.getSize());
            data.put("rows",allDailyReport.getDailyReportDTOS());
            data.put("status",200);
            data.put("msg","success!");
            data.put("total",allDailyReport.getTotal());
        } else {
            data.put("size",10);
            data.put("rows",null);
            data.put("status",-200);
            data.put("msg","no data!");
            data.put("total",0);
        }
        return data;
    }

    /**
     * 前端查询每日报表
     *
     * @param requestBody
     * @return
     */
    @RequestMapping("/edit")
    public ResultBody queryDetails(@RequestBody DailyReportRequestBody requestBody) {
        String dailyReportId = requestBody.getDailyReportId();
        DailyReportDTO dailyReportDTO = dailyReportService.queryDetails(dailyReportId);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), dailyReportDTO);

    }


    /**
     * 查看未提报商户
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/query_daily_store")
    public ResultBody queryDailyStore(@RequestBody DailyReportRequestBody requestBody) {
        List<NoSupportStoreDTO> objectList = dailyReportService.queryDailyStore(requestBody);
        if (objectList.size()<=0){
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(),objectList);
    }


}

